package com.zhang.web.services;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.zhang.pojo.Preferential;
import com.zhang.pojo.Shop;

public class PreferentialDAO {
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	public List<Preferential> finaAll(){
		List<Preferential> list = sessionFactory.openSession().createQuery("from Preferential").list();
		if(list.size()>0)
			return list;
		else
			return null;
	}
	
	public List<Preferential> findByShop(Shop shop){
		List<Preferential> list = (List<Preferential>) this.sessionFactory.openSession().createQuery("from Preferential preferential where preferential.shop = ?")
				.setParameter(0, shop).list();
		if(list.size()>0)
			return list;
		else
			return null;
	}
	
	public void update(String isAudit,String id){
		Query queryObject = sessionFactory.openSession().createQuery("update from Preferential " +
				"preferential set preferential.isAudit = ? where preferential.id = ?");
		queryObject.setParameter(0, isAudit).setString(1, id).executeUpdate();
	}
	
	public void insert(Preferential preferential){
		this.sessionFactory.openSession().save(preferential);
	}
}
